package com.wl.day74.biz;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wl.day74.dao.BookDAO;
import com.wl.day74.dao.BookTypeDAO;
import com.wl.day74.model.BookType;
import com.wl.day74.model.Books;

@Service("bookBiz")
@Scope("prototype")
public class BookBiz {
	@Resource
	private BookDAO bookDAO;
	
	@Autowired
	private BookTypeDAO bookTypeDAO;
	
	public void setBookTypeDAO(BookTypeDAO bookTypeDAO) {
		this.bookTypeDAO = bookTypeDAO;
	}

	public void setBookDAO(BookDAO bookDAO) {
		this.bookDAO = bookDAO;
	}

	public List<Books> findAll() {
		return bookDAO.findAll();
	}

	public void save(Books book) {
		book.setCreateDate(new Date());
		bookDAO.insert(book);
	}

	

	public void update(Books book) {
		book.setCreateDate(new Date());
		bookDAO.update(book);
	}

	public Books findById(int bookId) {
		return bookDAO.findById(bookId);
	}

	@Transactional
	public void delById(int bookId) {
		bookDAO.delete(bookId);
	}

	public void saveOrUpdate(Books book) {
		book.setCreateDate(new Date());
		if(book != null && book.getBookId() != null && book.getBookId() != 0){
			bookDAO.update(book);
		}else{
			bookDAO.insert(book);
		}
	}

}
